﻿using Learun.Util;
using Learun.Util.SqlSugar;
using System;

namespace Learun.Application.TwoDevelopment.ZZDT_EC
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创 建：超级管理员
    /// 日 期：2022-06-21 15:32
    /// 描 述：关联类型表
    /// </summary>
    public class ec_PanelStripTermService
    {
        #region 仓储
        Repository<ec_projectEntity> _projectRepository => new Repository<ec_projectEntity>();
        Repository<ec_PanelStripTermEntity> _panelStripTermRepository => new Repository<ec_PanelStripTermEntity>();
        #endregion
        /// <summary>
        /// 获取实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public ec_PanelStripTermEntity GetEntity(string keyValue, string ProjectId)
        {
            try
            {
                var tableName = ProjectSugar.TableName<ec_PanelStripTermEntity>(ProjectId);
                return _panelStripTermRepository.AsQueryable().AS(tableName).First(x => x.TermID == keyValue);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }
        #region 提交数据
        /// <summary>
        /// 删除实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void DeleteEntity(string ProjectId, string keyValue)
        {
            SqlSugarHelper.Db.BeginTran();
            try
            {
                //关联类型删除时，删除工程数据关联表记录。
                var tableName = ProjectSugar.TableName<ec_PanelStripTermEntity>(ProjectId);
                _panelStripTermRepository.AsDeleteable().AS(tableName).Where(x => x.TermID == keyValue).ExecuteCommand();
                SqlSugarHelper.Db.CommitTran();
            }
            catch (Exception ex)
            {
                SqlSugarHelper.Db.RollbackTran();
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }
        /// <summary>
        /// 保存实体数据（新增、修改）
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void SaveEntity(string ProjectId, string keyValue, ec_PanelStripTermEntity entity)
        {
            try
            {
                var ProjectEntity = new ec_projectEntity();
                if (!string.IsNullOrWhiteSpace(ProjectId))
                {
                    ProjectEntity = _projectRepository.GetById(ProjectId);
                }

                if (!string.IsNullOrEmpty(keyValue))
                {

                        entity.Modify(keyValue);
                        if (string.IsNullOrWhiteSpace(ProjectId))
                        {
                            _panelStripTermRepository.Update(entity);
                        }
                        else
                        {
                        //this.BaseRepository().UpdateByNoMap(entity, $"ec_Panel_Strip_Term_{ProjectEntity.ProjectIndex}", "TermID");
                        _panelStripTermRepository.AsUpdateable(entity).
                            IgnoreColumns(x => x.CreateTime).
                            IgnoreColumns(x => x.CreateUserID).AS($"ec_Panel_Strip_Term_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                    }

               
                }
                else
                {
                    entity.Create();
                   
                    if (string.IsNullOrWhiteSpace(ProjectId))
                    {
                        _panelStripTermRepository.Insert(entity);
                    }
                    else
                    {
                        //this.BaseRepository().InsertByNoMap(entity, $"ec_Panel_Strip_term_{ProjectEntity.ProjectIndex}");
                        _panelStripTermRepository.AsInsertable(entity).AS($"ec_Panel_Strip_Term_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        #endregion

    }
}
